﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;

namespace jQueryDotNetLib.Extenders
{
    [ToolboxItem(true)]
    [ToolboxData("<{0}:AjaxExtender TriggerSelector=\"\" MethodName=\"\" Selector=\"\" Event=\"\" runat=server />")]
    public class AjaxExtender : Control
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string ServerType
        {
            get
            {
                if (ViewState["ServerType"] == null) return "";
                return (string)ViewState["ServerType"];
            }

            set
            {
                ViewState["ServerType"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TriggerSelector
        {
            get
            {
                if (ViewState["TriggerSelector"] == null) return "";
                return (string)ViewState["TriggerSelector"];
            }

            set
            {
                ViewState["TriggerSelector"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Event
        {
            get
            {
                if (ViewState["Event"] == null) return "click";
                return (string)ViewState["Event"];
            }

            set
            {
                ViewState["Event"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Type
        {
            get
            {
                if (ViewState["Type"] == null) return "";
                return (string)ViewState["Type"];
            }

            set
            {
                ViewState["Type"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string MethodName
        {
            get
            {
                if (ViewState["MethodName"] == null) return "";
                return (string)ViewState["MethodName"];
            }

            set
            {
                ViewState["MethodName"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Callback
        {
            get
            {
                if (ViewState["Callback"] == null) return "";
                return (string)ViewState["Callback"];
            }

            set
            {
                ViewState["Callback"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TriggerEvent
        {
            get
            {
                if (ViewState["TriggerEvent"] == null) return "get";
                return (string)ViewState["TriggerEvent"];
            }

            set
            {
                ViewState["TriggerEvent"] = value;
            }
        }
        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientBeforeAjax
        {
            get
            {
                if (ViewState["OnClientBeforeAjax"] == null) return "";
                return (string)ViewState["OnClientBeforeAjax"];
            }

            set
            {
                ViewState["OnClientBeforeAjax"] = value;
            }
        }
        string resolvedTriggerSelector = null;
        public string ResolvedTriggerSelector
        {
            get
            {
                if (resolvedTriggerSelector == null)
                    resolvedTriggerSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.TriggerSelector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedTriggerSelector;
            }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (ServerType == "")
            {
                ServerType = string.Format("{0}; {1}", Page.GetType().FullName, Page.GetType().Assembly.FullName);
            }
            
            writer.Write("<script language=\"javascript\">");
            writer.Write("jQuery(\"" + ResolvedTriggerSelector + "\").bind('" + Event + "',");
            writer.Write("function() {  var dataVal = '';");
            if(OnClientBeforeAjax!="")
            {
                writer.Write("dataVal = " + OnClientBeforeAjax);
            }
            writer.Write("jQuery." + TriggerEvent + "('jQueryAjax.ashx', {rnd:Math.random, data: dataVal, type:'" + ServerType + "', method:'" + MethodName + "'}, " + Callback + ", '" + Type + "');");
            writer.Write("});");
            writer.Write("</script>");
        }
    }
}